package com.shumi.rbac.web;



import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shumi.enums.StaticVarEnum;
import com.shumi.fun.ReturnEnumTransform;
import com.shumi.fun.WebResult;
import com.shumi.rbac.model.User;
import com.shumi.rbac.service.AuthService;
//import com.shumi.rbac.service.AuthService;
import com.shumi.rbac.service.JwtTokenService;
import com.shumi.rbac.service.SystemService;

@Slf4j
@RestController
@RequestMapping(value = "/auth")
public class AuthController{

    @Autowired
    AuthService authService;

    @Autowired
    SystemService systemService;

    @Autowired
    JwtTokenService tokenService;

    WebResult webResult;

    @RequestMapping(value = "/login")

    @ResponseBody
    public Map login(String logincode, String password, HttpServletResponse response,HttpSession session){
        HashMap map = new HashMap();
        User user = systemService.getUserByLoginName(logincode);
        if(systemService.validatePassword(password,user.getPassword()))
            session.setAttribute("user",user);
        else{
            map.put("status","400");
            map.put("message","密码错误");
            return map;
        }
        map.put("status", "200");
        map.put("message", "登录成功");
        return map;


//        AuthService.ReturnEnum returnEnum = authService.login(logincode, password);
//        webResult=ReturnEnumTransform.transform(returnEnum);
//        if(webResult.isSuccess()){
//            JwtTokenService.ReturnEnum tmp=tokenService.generator(logincode,new Date());
//            if(tmp.isSuccess()){
//                response.addHeader(StaticVarEnum.TOKEN.getKey(),(String) tmp.getReturnObject());
//            }
//            return  webResult;
//        }
//        return webResult;
    }
    
    
    

    public WebResult login(String logincode, String password, HttpServletResponse response) {
        AuthService.ReturnEnum returnEnum = authService.login(logincode, password);
        webResult=ReturnEnumTransform.transform(returnEnum);
        if(webResult.isSuccess()){
            JwtTokenService.ReturnEnum tmp=tokenService.generator(logincode,new Date());
            if(tmp.isSuccess()){
                response.addHeader(StaticVarEnum.TOKEN.getKey(),(String) tmp.getReturnObject());
            }
            return  webResult;
        }
        return webResult;
    }


    @RequestMapping(value = "/logout/{token}")
    public WebResult logout(@PathVariable String token){
        JwtTokenService.ReturnEnum returnEnum = tokenService.destory(token);
        webResult=ReturnEnumTransform.transform(returnEnum);
        return webResult;
    }

    
    /**
     * 不加密的密码登录验证方式
     * @param token
     * @return
     */
    @RequestMapping(value = "/login1")
    @ResponseBody
    public Map login1(String loginName, String password, HttpServletResponse response,HttpSession session){
        HashMap map = new HashMap();
        User user = systemService.getUserByLoginName1(loginName);
        if(password.equals(user.getPassword())){
        	session.setAttribute("user",user);
        }  
        else{
            map.put("status","400");
            map.put("message","密码错误");
            return map;
        }
        map.put("status", "200");
        map.put("message", "登录成功");
        map.put("user：", user.getLoginName());
        return map;
    }
    
    /**
     * 注销功能
     */
    @RequestMapping(value="/logout")
    @ResponseBody
    public Map logOut(HttpServletResponse response,HttpSession session){
    	HashMap map = new HashMap();
    	session.removeAttribute("user");
        map.put("status", "200");
        map.put("message", "退出成功");
        return map;
    	
    }
    
    


}
